/* 引入  临时身份标识 */
import { getUserTempId } from "../../utils/userAbout"
import { getCode, getLogin, getLogout, getRegister, getUserAddressList, getUserInfo } from "../../api"
const state = {
    /* 临时标识 */
    userTempId: getUserTempId,
    code: "",//验证码
    token: localStorage.token_key || "", //请求登录时服务器返回的
    userInfo: "",//用户信息
    userAddressList: ""//用户地址列表
}
const actions = {
    /* 获取手机验证码 */
    async reqCode({ commit }, phone) {
        const result = await getCode(phone);
        if (result.code === 200) {
            commit("RECEIVE_CODE", result.data)
            return result.data
        } else {
            return Promise.reject("failed！！")
        }
    },
    /* 用户注册 */
    async reqRegister({ commit }, userInfo) {
        const result = await getRegister(userInfo);
        // console.log(result);
        if (result.code === 200) {
            return "ok"
        } else {
            /* 返回错误信息 */
            return Promise.reject(result.message)
        }
    },
    /* 登录请求 接收服务器返回的token */
    async reqLogin({ commit }, userInfo) {
        const result = await getLogin(userInfo);
        if (result.code === 200) {
            /* 登录请求成功 将返回的token提交给mutations来操作  */
            commit("RECEIVE_LOGINTOKEN", result.data.token);
            /* 将token存储到localstorage */
            localStorage.token_key = result.data.token;
            return "ok";
        } else {
            /* 登录请求失败 将失败原因返回给组件 */
            return Promise.reject(result.message);
        }
    },
    /* 根据token获取用户信息 */
    async reqUserInfo({ commit }) {
        const result = await getUserInfo();
        if (result.code === 200) {
            commit("RECEIVE_USERINFO", result.data)
        } else {
            return Promise.reject("获取用户数据出错error")
        }
    },
    /*  退出登录请求  */
    async reqLogout({ commit }) {
        const result = await getLogout()
        if (result.code === 200) {
            return "ok"
        } else {
            return Promise.reject("failed")
        }
    },
    /* 清空token和用户信息 userInfo */
    reqClearToken({ commit }) {
        /* 清除vuex中的 */
        commit("CLEAR_TOKENANDUSERINFO")
        /* 清除localStorage中的 */
        delete localStorage.token_key
    },
    /* 获取用户地址信息 */
    async reqUserAddressList({ commit }) {
        const result = await getUserAddressList()
        if (result.code === 200) {
            commit("RECEIVE_USERADDRESSLIST", result.data)
        } else {
            return Promise.reject("地址获取出错")
        }
    }

}
const mutations = {
    /* 将请求获取的验证码放在state中 */
    RECEIVE_CODE(state, code) {
        state.code = code
    },
    /* 将请求登录返回的token 存入state中 */
    RECEIVE_LOGINTOKEN(state, token) {
        state.token = token
    },
    /* 获取用户信息 */
    RECEIVE_USERINFO(state, userInfo) {
        state.userInfo = userInfo
    },
    /* 清除token和用户信息 userInfo */
    CLEAR_TOKENANDUSERINFO(state) {
        state.token = "",
            state.userInfo = ""
    },
    /* 存储用户地址 */
    RECEIVE_USERADDRESSLIST(state, userAddressList) {
        state.userAddressList = userAddressList
    }
}
const getters = {}

export default {
    state,
    actions,
    mutations,
    getters
}